package djboom.materials.display.page.work3 {
	import djboom.materials.Work;
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.core.DataCore;
	import djboom.materials.display.elements.SampleSelectable;
	import djboom.materials.display.elements.RoundedButton;
	import djboom.materials.display.elements.Window;
	import djboom.materials.display.page.Page;
	import djboom.materials.events.PageEvent;
	import djboom.materials.events.SampleSelectableEvent;
	import djboom.materials.events.WindowEvent;

	import org.casalib.display.CasaBitmap;
	import org.casalib.display.CasaSprite;

	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 06.04.2010
	 * 
	 */

	public class Page2 extends Page {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private static var _selectedCounter:int = 0;
		
		private var _textBitmap:CasaBitmap;
		private var _samplesList:XMLList;
		private var _samples:CasaSprite;

		private var _window:Window;

		private var _simpleButton:RoundedButton;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function Page2() {
			super();
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------
		private function incrementCounter(event:SampleSelectableEvent):void {
			DataCore.selectableSample(DataCore._materialsWork3XML, event.sampleName, true);
            
			_selectedCounter++;
            
			checkSelectedSample();
		}

		private function decrementCounter(event:SampleSelectableEvent):void {
			DataCore.selectableSample(DataCore._materialsWork3XML, event.sampleName, false);

			_selectedCounter--;
            
			checkSelectedSample();
		}

		private function checkSelectedSample():void {
			if (_selectedCounter >= 1) {
				if (_simpleButton == null) {
					_simpleButton = new RoundedButton("Продолжить");
					_simpleButton.x = Work.WIDTH * 0.5 - _simpleButton.width * 0.5;
					_simpleButton.y = Work.HEIGHT - 118;
					_simpleButton.addEventListener(MouseEvent.CLICK, changePage);
					addChild(_simpleButton);
				}           
			} else {
				if (_simpleButton != null) {
					_simpleButton.destroy();
					_simpleButton = null;
				}
			}
		}

		private function changePage(event:MouseEvent):void {
			if (_selectedCounter > 1) {
				lockPage();
                
				_window = new Window();
				_window.addEventListener(WindowEvent.CLOSE, closeWindow);
				_window.init("Внимание!", "Необходимо выбрать только 1 образец.");
				addChild(_window);
			} else {
				var pageEvent:PageEvent = new PageEvent(PageEvent.CHANGE);
				pageEvent.pageName = Page.WORK_3_PAGE_3;            
				dispatchEvent(pageEvent);
			}
		}

		private function closeWindow(event:WindowEvent):void {
			unlockPage();
            
			_window.destroy();
			_window = null;
		}

		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		override public function init():void {
			// Создаем заголовок страницы.
			createTitleWork(WORK3_NAME, 3);
            
			_textBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.WORK2_PAGE2_TEXT));
			_textBitmap.x = 38;
			_textBitmap.y = 100;
			addChild(_textBitmap);
			
			DataCore.initWork3();
            
			_samplesList = DataCore._materialsWork3XML.child("material");

			_samples = new CasaSprite();
             
			for each (var sampleXML:XML in _samplesList) {
				var sampleName:String = sampleXML.attribute("name").toString();
				var sampleBitmapData:String = sampleXML.attribute("bitmapdata").toString();
            
				var sampleSelectable:SampleSelectable = new SampleSelectable(sampleName, BitmapDataCore.getBitmapData(sampleBitmapData));
				sampleSelectable.x = 230 * (sampleXML.childIndex() % 4);
				sampleSelectable.addEventListener(SampleSelectableEvent.SELECT, incrementCounter);
				sampleSelectable.addEventListener(SampleSelectableEvent.DESELECT, decrementCounter);
				_samples.addChild(sampleSelectable);
			}
            
			_samples.x = Work.WIDTH * 0.5 - _samples.width * 0.5;
			_samples.y = _textBitmap.y + _textBitmap.height + 30;
			addChild(_samples);         
		}

		override protected function clearPage(event:Event):void {
            super.clearPage(event);
			
			while (_samples.numChildren) {
				var sampleSelectable:SampleSelectable = _samples.getChildAt(0) as SampleSelectable;
                
				sampleSelectable.removeChildrenAndDestroy();
				sampleSelectable = null;
			}
            
			_samples.destroy();
			_samples = null;
            
            if (_simpleButton != null) {
            	_simpleButton.destroy();
            	_simpleButton = null;
            }
            
			_textBitmap.destroy();
			_textBitmap = null;
            
			_samplesList = null;
		}
        
		// ----------------------------------------------------------------------------
        // Публичные свойства.
        // ----------------------------------------------------------------------------
	}
}
